package yang.yu.sessioncache.servlet;

import yang.yu.sessioncache.SessionService;
import yang.yu.sessioncache.redis.RedisClient;
import yang.yu.sessioncache.redis.RedisSessionService;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Created by yyang on 15/9/8.
 */
public class MyHttpSessionListener implements HttpSessionListener {

    private RedisClient redisClient = createRedisClient();
    private SessionService sessionService;

    private RedisClient createRedisClient() {
        return new RedisClient();
    }

    SessionService getSessionService() {
        return sessionService;
    }

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        String sessionId = httpSessionEvent.getSession().getId();
        sessionService = new RedisSessionService(sessionId, redisClient);
        sessionService.init();
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        String sessionId = httpSessionEvent.getSession().getId();
        sessionService = new RedisSessionService(sessionId, redisClient);
        sessionService.shutdown();
    }
}
